package cn.hl.sfzy.service.impl;

import cn.hl.sfzy.entity.*;
import cn.hl.sfzy.mapper.*;
import cn.hl.sfzy.service.CourseMarkService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName CourseMarkServiceImpl
 * @Description TODO
 * @Author libowen
 * @Date 2024年10月26日 14:23
 * @Version 1.0
 */
@Service
public class CourseMarkServiceImpl implements CourseMarkService {
    @Autowired
    private CourseMarkMapper courseMarkMapper;

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public PageInfo<CourseMark> findByCourseId(int pageNum, int pageSize,Long course_id) {
        PageHelper.startPage(pageNum, pageSize);
        List<CourseMark> list = courseMarkMapper.selectByCourseId(course_id);
        for(CourseMark cm : list){
            User user =
                    userMapper.selectById(cm.getUser_id());
            cm.setUser(user);
        }
        PageInfo<CourseMark> info = new PageInfo<>(list);
        return info;
    }

    @Override
    public int deleteCourseMark(Long id) {
        int delete = courseMarkMapper.delete(id);
        return delete;
    }

    @Override
    public int deleteCourseMarkByUserId(Long user_id) {
        int delete = courseMarkMapper.deleteByUserId(user_id);
        return delete;
    }

    @Override
    public int addCourseMark(CourseMark courseMark) {
        int insert = courseMarkMapper.insert(courseMark);
        return insert;
    }

    @Override
    public int updateCourseMark(CourseMark courseMark) {
        int update = courseMarkMapper.update(courseMark);
        return update;
    }

    @Override
    public PageInfo<CourseMark> findByPage(int pageNum, int pageSize, CourseMark courseMark) {
        PageHelper.startPage(pageNum, pageSize);
        List<CourseMark> list = courseMarkMapper.select(courseMark);
        // 遍历课程内容，并根据课程内容中的typeId来查询对应的文章类型对象
        for(CourseMark cm : list){
            Course course =
                    courseMapper.selectById(cm.getCourse_id());
            cm.setCourse(course);
        }
        for(CourseMark cm : list){
            User user =
                    userMapper.selectById(cm.getUser_id());
            cm.setUser(user);
        }
        PageInfo<CourseMark> info = new PageInfo<>(list);
        return info;
    }
}
